package com.gilight.base.controller;


import com.gilight.base.admin.order.AdminOrderService;
import com.gilight.base.base.ResultEntity;
import com.gilight.base.base.exception.ServiceException;
import com.gilight.base.dto.order.OrderDTO;
import com.gilight.base.dto.order.OrderStatisticsByDayDTO;
import com.gilight.base.dto.order.OrderStatisticsDTO;
import com.gilight.base.model.CouponDO;
import com.gilight.base.model.Page;
import com.gilight.base.model.order.OrderDO;
import com.gilight.base.service.SysUserZzjgService;
import com.gilight.base.service.coupon.AdminCouponService;
import com.gilight.base.util.MapUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


/**
 * @author liken
 * 商品管理控制类
 */
@RestController
@RequestMapping("/web/admin-coupon")
public class AdminCouponController {

    @Autowired
    private AdminCouponService adminCouponService;
    @Autowired
    private SysUserZzjgService sysUserZzjgService;

    /**
     *
     * @param title
     * @param type
     * @param status
     * @param pageNo
     * @param limit
     * @param zzjgId
     * @return
     * @throws ServiceException
     */
    @ApiOperation(value = "优惠卷列表")
    @RequestMapping(value = "/list", method = {RequestMethod.GET,RequestMethod.POST})
    public ResultEntity<Page<CouponDO>> list(
            String title,

            Integer type,
            Integer status,
            Integer pageNo,
            Integer limit,
            Long zzjgId)
                throws ServiceException{
        ResultEntity resultEntity = ResultEntity.success();
        List<Map<String,Object>> zzjgsUser = sysUserZzjgService.getCurrentUserZzjgs();
        if(zzjgId == null){
             zzjgId = 1L;
            if (zzjgsUser != null && zzjgsUser.size() > 0) {
                zzjgId =  MapUtils.getLong(zzjgsUser.get(0), "zzjgId");
            }
        }

        resultEntity.setData(adminCouponService.list(title,type,status,pageNo,limit,zzjgId));
        return resultEntity;

    }


    /**
     * 创建优惠卷
     * @return
     * @throws ServiceException
     */
    @ApiOperation(value = "创建优惠卷")
    @RequestMapping(value = "/create")
    public ResultEntity create(
            @RequestBody CouponDO couponDO
            ) throws ServiceException {
        List<Map<String, Object>> zzjgsUser = sysUserZzjgService.getCurrentUserZzjgs();
        ResultEntity resultEntity = new ResultEntity();
        try {
            couponDO.setCouponSendRule1("");
            if (zzjgsUser != null && zzjgsUser.size() > 0) {
                Long zId = MapUtils.getLong(zzjgsUser.get(0), "zzjgId");
                couponDO.setZzjgId(zId);
            }else{
                couponDO.setZzjgId(1L);
            }
            resultEntity.setData(adminCouponService.create(couponDO));
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultEntity;
    }
    /**
     * 修改优惠卷
     * @return
     * @throws ServiceException
     */
    @ApiOperation(value = "创建优惠卷")
    @RequestMapping(value = "/edit")
    public ResultEntity edit(
            @RequestBody CouponDO couponDO
    ) throws ServiceException {
        List<Map<String, Object>> zzjgsUser = sysUserZzjgService.getCurrentUserZzjgs();
        ResultEntity resultEntity = new ResultEntity();
        couponDO.setCouponSendRule1("");
        resultEntity.setData(adminCouponService.edit(couponDO));
        return resultEntity;
    }
    @ApiOperation(value = "删除")
    @RequestMapping(value = "/delete")
    public ResultEntity  delete(
            Long couponId) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(adminCouponService.delete(couponId));
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return resultEntity;
    }
    @ApiOperation(value = "删除")
    @RequestMapping(value = "/update-status")
    public ResultEntity  delete(
            Long couponId,
            Integer status) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(adminCouponService.updateCouponStatus(couponId,status));
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return resultEntity;
    }
}
